package com.ym.service.impl;

import com.ym.dto.DeviceResDto;
import com.ym.enums.DeviceEnum;
import com.ym.mapper.DeviceMapper;
import com.ym.domain.Device;
import com.ym.service.IDeviceService;
import com.ym.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceServiceimpl implements IDeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    public DeviceResDto queryDeviceById(Long deviceId) {
        // 1.查询设备
        Device device = deviceMapper.findById(deviceId);
        if (device == null){
            return new DeviceResDto(deviceId + "号设备不存在");
        }
        DeviceResDto resDto = new DeviceResDto();
        int typeValue = device.getType();
        DeviceEnum deviceType = DeviceEnum.values()[typeValue];
        String typeName = deviceType.getName();
        resDto.setName(device.getName());
        resDto.setType(typeName);
        resDto.setBuildId(device.getBuildId());
        resDto.setFloorId(device.getFloorId());
        // 4.返回
        return resDto;
    }

    @Override
    public JSONResult add(Device device) {
        String a = "";
        if (device.getType() == null || "".equals(device.getType())) {
            a =a + "类型不能为空 ";
        }
        if (device.getName() == null || "".equals(device.getName())) {
            a =a + "描述不能为空 ";
        }
        if (device.getBuildId() == null || "".equals(device.getBuildId())) {
            a =a + "楼栋不能为空 ";
        }
        if (device.getFloorId() == null || "".equals(device.getFloorId())) {
            a =a + "楼层不能为空 ";
        }
        if (device.getType() == null || "".equals(device.getType()) ||
                device.getName() == null || "".equals(device.getName()) ||
                device.getBuildId() == null || "".equals(device.getBuildId()) ||
                device.getFloorId() == null || "".equals(device.getFloorId())) {
            return JSONResult.error(a);
        }

        deviceMapper.add(device);
        return new JSONResult();
    }

    @Override
    public List<Device> getDeviceList(Long id,Long floorId) {
        return deviceMapper.getDeviceList(id,floorId);
    }

    @Override
    public List<Device> getDeviceListByType(Long type) {
        return deviceMapper.getDeviceListByType(type);
    }
}
